package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class workSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /*工作台今日数据查询*/
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /*
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        HashMap<Object, Object> conditionMap = new HashMap<>();
        conditionMap.put("begin", begin); // 查询条件，开始时间
        conditionMap.put("end", end);  // 查询条件，结束时间

        // 1. 查询总订单数量
        Integer totalOrderCount = orderMapper.countByMap(conditionMap);
        // 2. 添加查询条件，查询已完成的订单数量
        conditionMap.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countByMap(conditionMap);
        // 3. 同时查询营业额
        Double turnover = orderMapper.sumByMap(conditionMap);
        turnover = (turnover == null ? 0.0 : turnover); // 避免空指针异常
        // 4. 客单价 每个订单的平均价格
        Double unitPrice = 0.0; // 客单价
        Double orderCompletionRate = 0.0; // 订单完成率
        if (totalOrderCount != 0 && validOrderCount != 0) {
            // 计算订单完成率
            orderCompletionRate = validOrderCount / totalOrderCount.doubleValue();
            // 计算订单客单价
            unitPrice = turnover / validOrderCount;
        }
        // 计算新增用户数
        Integer newUsers = userMapper.countByMap(conditionMap);
        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /*
     * 查询订单管理方法
     * */
    @Override
    public OrderOverViewVO getOrderOverView() {
        // 设置查询条件map
        HashMap<Object, Object> conditionMap = new HashMap<>();
        // 获得当天的开始时间
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
        conditionMap.put("begin", begin);
        // 2.获取待接单数量
        conditionMap.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrdersNumber = orderMapper.countByMap(conditionMap);
        // 3.获取待派送订单数量
        conditionMap.put("status", Orders.CONFIRMED);
        Integer deliveredOrdersNumber = orderMapper.countByMap(conditionMap);
        // 4.获取已完成订单数量
        conditionMap.put("status", Orders.COMPLETED);
        Integer completedOrdersNumber = orderMapper.countByMap(conditionMap);
        // 5.获取已取消订单数量
        conditionMap.put("status", Orders.CANCELLED);
        Integer cancelledOrdersNumber = orderMapper.countByMap(conditionMap);
        // 6. 获取总订单数量
        conditionMap.put("status", null);
        Integer allOrdersNumber = orderMapper.countByMap(conditionMap);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrdersNumber)
                .deliveredOrders(deliveredOrdersNumber)
                .completedOrders(completedOrdersNumber)
                .cancelledOrders(cancelledOrdersNumber)
                .allOrders(allOrdersNumber)
                .build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        // 设置查询条件map
        HashMap<Object, Object> conditionMap = new HashMap<>();
        // 1. 查询在售菜品数量
        conditionMap.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(conditionMap);
        // 2. 查询停售菜品数量
        conditionMap.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(conditionMap);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public DishOverViewVO getSetmealOverView() {
        // 设置查询条件map
        HashMap<Object, Object> conditionMap = new HashMap<>();
        // 1. 查询在售套餐数量
        conditionMap.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(conditionMap);
        // 2. 查询停售套餐数量
        conditionMap.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(conditionMap);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
